//
//  YHObserverPracticeVC.m
//  LyhToolsTest
//
//  Created by nice on 2019/2/13.
//  Copyright © 2019 LYH.com. All rights reserved.
//

#import "YHObserverPracticeVC.h"


@interface YHObserverPracticeVC ()
@property (nonatomic, strong) UIButton *addBtn;//加
@property (nonatomic, strong) UIButton *reductBtn;//减
@property (nonatomic, strong) UILabel  *showValue;

@property (nonatomic, assign)  NSInteger  weekNum;//星期几
@end

@implementation YHObserverPracticeVC
/**
 *_weekNum 与 self.weekNum的区别
 *使用self.XXX 会调用set和get方法，而_XXX则直接访问实例变量并赋值，不会调用set和get方法
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNava];
    [self addBtn];
    [self reductBtn];
//    self.model = [[weekNumModel alloc]init];
    self.weekNum = 1;
   
    self.showValue.text = [NSString stringWithFormat:@"星期%ld",self.weekNum];
    [self printinfo];
    
    [self addObserver:self forKeyPath:@"weekNum" options:NSKeyValueObservingOptionNew context:nil];
   
    
}
- (void)initNava
{
    WeakSelf;
    wself.ts_navgationBar = [TSNavigationBar navWithTitle:@"观察者模式" backAction:^{
        [wself.navigationController popViewControllerAnimated:YES];
    }];
}


#pragma -mark ————————————————————Action————————————————————
- (void)BtnClick:(UIButton *)send
{
    switch (send.tag) {
        case 10:
        {
            if (self.weekNum == 7) {
                self.weekNum = 7;
            }else{
                self.weekNum++;
            }
        }
            break;
            
        default:
            if ( self.weekNum == 1) {
                self.weekNum = 1;
            } else {
                 self.weekNum--;
            }
            break;
    }
    self.showValue.text = [NSString stringWithFormat:@"星期%ld",self.weekNum];
    
     [self printinfo];
}
#pragma -mark ——————————监听代理-——————————
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"weekNum"]) {
        NSLog(@"%@",[object valueForKey:@"weekNum"]);
        NSLog(@"--change:--%@", [change objectForKey:@"new"]);
        NSString *changeStr = [NSString stringWithFormat:@"%@",[change objectForKey:@"new"]];
        [self changeColorForvalve:changeStr.integerValue];
    }
}
- (void)changeColorForvalve:(NSInteger )changevalve
{
//    self.weekNumType = changevalve;
    switch (changevalve) {
        case 1:
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor orangeColor];
            break;
        case 3:
            self.view.backgroundColor = [UIColor yellowColor];
            break;
        case 4:
            self.view.backgroundColor = [UIColor greenColor];
            break;
        case 5:
            self.view.backgroundColor = [UIColor blueColor];
            break;
        case 6:
            self.view.backgroundColor = [UIColor grayColor];
            break;
        default:
             self.view.backgroundColor = [UIColor purpleColor];
            break;
    }
}

#pragma -mark ————————————————————Lay————————————————————
- (UIButton *)addBtn
{
    if (!_addBtn) {
        _addBtn =  [[UIButton alloc]initWithFrame:CGRectMake(50, SafeAreaTopHeight+20, 50, 50)];
        _addBtn.title = @"+";
        _addBtn.titleFont = 25;
        _addBtn.titleColor = [UIColor greenColor];
        [_addBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        _addBtn.tag = 10;
        [_addBtn addTarget:self action:@selector(BtnClick:)];
        [self.view addSubview:_addBtn];
    }
    return _addBtn;
}
- (UIButton *)reductBtn
{
    if (!_reductBtn) {
        _reductBtn =  [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-100, SafeAreaTopHeight+20, 50, 50)];
        _reductBtn.title = @"-";
        _reductBtn.titleFont = 25;
        _reductBtn.titleColor = [UIColor redColor];
        _reductBtn.tag = 11;
        [_reductBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
         [_reductBtn addTarget:self action:@selector(BtnClick:)];
        [self.view addSubview:_reductBtn];
    }
    return _addBtn;
}
- (UILabel *)showValue
{
    if (!_showValue) {
        _showValue = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/2-50, SafeAreaTopHeight+35, 100, 30)];
        _showValue.font = [UIFont systemFontOfSize:25];
        _showValue.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_showValue];
    }
    return _showValue;
}

//打印
- (void)printinfo
{
    NSLog(@"isa-> %@", object_getClass(self));
}
- (void)dealloc
{
    //移除观察者
    [self removeObserver:self forKeyPath:@"weekNum"];
    [self printinfo];
}
@end



